<template>
  <div class="article-comments">
    <!-- {{$route.params.id}} -->
    <!-- 评论列表 -->
    <van-list v-model="loading"
              :finished="finished"
              finished-text="没有更多了"
              @load="onLoad">
      <van-cell v-for="item in list"
                :key="item.com_id.toString()">
        <van-image slot="icon"
                   round
                   width="30"
                   height="30"
                   style="margin-right: 10px;"
                   :src="item.aut_photo" />
        <span style="color: #466b9d;"
              slot="title">{{item.aut_name}}</span>
        <div slot="label">
          <p style="color: #363636;">{{item.content}}</p>
          <p>
            <span style="margin-right: 10px;">{{item.pubdate | handlerRelativeTime}}</span>
            <van-button size="mini"
                        type="default"
                        @click="commentReply(item)">回复{{item.reply_count}}</van-button>
          </p>
        </div>
        <van-icon slot="right-icon"
                  name="like-o" />
      </van-cell>
    </van-list>
    <!-- 评论列表 -->

    <!-- 发布评论 -->
    <van-cell-group class="publish-wrap">
      <van-field clearable
                 placeholder="请输入评论内容"
                 v-model.trim="content">
        <van-button slot="button"
                    size="mini"
                    type="info"
                    @click="addComment">发布</van-button>
      </van-field>
    </van-cell-group>
    <!-- /发布评论 -->
    <!-- 评论回复的弹出层 -->
    <van-popup v-model="replyShow"
               position="bottom"
               style="height: 80%"
               round>
      <comment-reply v-if="replyShow"
                     :currentComment="currentComment"></comment-reply>
    </van-popup>
    <!-- /评论回复的弹出层 -->
  </div>
</template>

<script>
import { reqGetArticleComments, reqAddComment } from '@/api/comments'
import CommentReply from './CommentReply'
export default {
  name: 'ArticleComment',
  props: {
    id: { // 文章的id
      type: String,
      required: true
    }
  },
  data () {
    return {
      list: [], // 评论列表
      loading: false, // 上拉加载更多的 loading
      finished: false, // 是否加载结束
      offset: null,
      content: '', // 评论内容
      replyShow: false, // 设置点击回复的弹层布尔值
      currentComment: {} // 点击回复按钮携带当前评论对象
    }
  },
  components: {
    CommentReply
  },
  methods: {
    async onLoad () {
      const data = await reqGetArticleComments('a', this.id, this.offset)
      // console.log(data) // data中有last_id 下一次请求要携带的分页的参数
      // 将数组push到list中
      const results = data.data.results
      this.list.push(...results)
      // 关闭loading
      this.loading = false
      // 覆盖offset
      this.offset = data.data.last_id
      // 判断数据是否加载完毕
      if (results.length === 0) {
        this.finished = true
      }
    },
    // 添加文章评论
    async addComment () {
      try {
        // 非空校验
        if (this.content === '') return
        const data = await reqAddComment(this.id, this.content)
        console.log(data) // data中有new_obj
        // 可以重新获取评论列表 直接将新增的评论unshift list中
        this.list.unshift(data.data.new_obj)
        // 清空文本框内容
        this.content = ''
        // 提示发表成功
        this.$toast.success('发表成功')
      } catch (error) {
        this.$toast.fail('此文章禁止评论')
      }
    },
    // 点击回复按钮展示弹出层
    commentReply (item) {
      // console.log(item)
      this.replyShow = true
      this.currentComment = item
    }
  }
}
</script>

<style scoped lang='scss'>
// 发表评论的区域是固定在下端的
.publish-wrap {
  position: fixed;
  left: 0;
  bottom: 0;
  width: 100%;
}
// 给发表评论区空出地方
.van-list {
  margin-bottom: 45px;
}
</style>
